<?php

namespace App\Http\Controllers;


use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Services\ConfigServices;
use App\Http\Services\RoomServices;
use Illuminate\Http\Request;
use App\Jobs\ExampleJob;


//打碟活动
class DishController extends Controller
{
    protected $configServices;
    protected $roomServices;

    public function __construct()
    {
        $this->configServices = new ConfigServices();
        $this->roomServices = new RoomServices();
    }

    //定时任务获取每小时开启房间  开启免费打碟
    public function getOpenRoom()
    {
        $this->checkDate();
        return $this->roomServices->getOpenRoom();
    }

    //发放免费 奖励   每小时前几分钟
    public function awardFreeGift()
    {
        $this->checkDate();
        $this->roomServices->freeAwardGift();
        $this->roomServices->awardPayGift();
    }

    //新开启房间  删除免费奖励
    public function newOpenRoom($body)
    {
        $this->checkDate();
        $room_id = $body['room_id'];
        return $this->roomServices->closeFreeDish($room_id);
    }

    /*
     * 点击参与打碟
     * @param $room_id 参与房间id
     * @param $user_id 参与用户id
     * @param $type 0免费 1收费
     * */
    public function clickAward(Request $request)
    {
        $this->checkDate();
        $room_id = $request->get('room_id', 0);
        $user_id = $request->get('user_id', 0);
        $level = $request->get('level', 0);
        $type = $request->get('type', 0);
        write_log('clickDish', '参与打碟', $request->input());//开启房间
        $res = $this->roomServices->clickDishAward($room_id, $user_id, $type, $level);
        OutJson::toJson($res['code'], $res['msg'], $res['data']);
    }

    //开启付费打碟房间
    public function openPayDishRoom($body)
    {
        $this->checkDate();

        $room_id = $body['room_id'] ?? 1;
            $minute = $body['time_minute'] ?? 0;
            $level = $body['type'] ?? 0;
            $pay_user_id = $body['user_id'] ?? 0;
            $this->roomServices->payDishRoom($room_id, $minute, $level, $pay_user_id);
            write_log('openPayDish', '开启打碟', $body);//开启房间
        return true;
    }

    //获取用户房间内打碟信息
    public function getUserDishInfo(Request $request)
    {
        $user_id = $request->input('user_id', 0);//用户id
        $room_id = $request->input('room_id', 0);//房间id
        $res = $this->roomServices->getUserDishInfo($room_id, $user_id);
        OutJson::toJson($res['code'], $res['msg'], $res['data']);
    }

    public function getGiftLog(Request $request)
    {
        $user_id = $request->input('user_id', 0);//用户id
        $res = $this->roomServices->getUserGiftLog($user_id);
        OutJson::toJson(200, '成功', $res);
    }

    public function jumpDishRoom(Request $request)
    {
        $res = $this->roomServices->jumpDishRoom();
        if ($res == false) {
            OutJson::toJson(500, '失败', []);
        }
        OutJson::toJson(200, '成功', $res);
    }

    public function createAwardPond(Request $request)
    {
        $level = $request->input('level', 0);//房间id
        $this->roomServices->createAwardPond($level);
    }

    public function popAwardPond(Request $request)
    {
        $level = $request->input('level', 0);//房间id
        $len = $request->input('len', 5000);//房间id
        $type = $request->input('type', 0);//房间id
        $this->roomServices->popAwardPond($level, $len, $type);
    }

    public function checkDate()
    {
        $check_date = $this->configServices->checkConfigDate(25, time(), 'dish_date');
        if (!$check_date) {
            write_log('dishTimeOut', 'timeError', []);
            die;
        }
    }

}

